﻿// JScript 文件

function AssExeTool(id, param)
{
//    if (!window.m_activeList)
//    {
//        alert("请激活一个属性页面!");
//        return;
//    }
    switch(parseInt(id))
    {
        case m_vt.ASS_SEL_PAGE_ALL :  // 选择本页
        {
            m_activeList.setCurPageChecked(1);
        }
        break;
        case m_vt.ASS_SEL_PAGE_NULL :  //取消本页 
        {
            m_activeList.setCurPageChecked(0);
        }
        break;
        case m_vt.ASS_SEL_ALL :  //选择所有 
        {
            m_activeList.setAllPagesChecked(1);
        }
        break;
        case m_vt.ASS_SEL_NULL :  //取消所有 
        {
             m_activeList.setAllPagesChecked(0);
        }
        break;
        case m_vt.ASS_EXC_EXP :  //excel导出
        case m_vt.ASS_EXC_IMP :  //excel导入
        case m_vt.ASS_IF_EXP :  //接口导出
        case m_vt.ASS_IF_IMP :  //接口导入
        case m_vt.ASS_EXC_EXP1:
        case m_vt.ASS_EXC_IMP1:
        {
            var arrRow = null;
            if (m_activeList.bchecked)
            {
                arrRow = m_activeList.getAllPagesChecked(0);
            }
            else
            {
                arrRow = m_activeList.getAllPagesChecked(1);
            }
            var arId = [];//取ID0列表，在
            var colLen = m_activeList.arrCol.length;
            for (var i =0; i < arrRow.length; i++)
            {
                arId.push(arrRow[i].arrCell[colLen - 1].text);
            }
            if (id == m_vt.ASS_EXC_EXP)
            {
                var kind = uiCfgToIoCfg(m_activeList.data.attStyle, false);
                var arrFld = getArrIoFld(kind);
                if (arId.length < 1 && m_activeList.bchecked != 1)
                {
                    top.CtnDlg.warning("请至少选择一项！");
                    return;
                }
                var assparam = "CheckedAll=" + m_activeList.bchecked
                            + "&Ids=" + arId.join(",")
                            + "&Sql=" + encodeURIComponent(m_activeList.data.strSql)
                            + "&IoCfgType=" + kind
                            + "&IoCfgXml=" + encodeURIComponent(objListToXmlStr(arrFld));
                CtnExcel.ExpByUser("ASS_ExpToExecl", assparam);
            }
            else if (id == m_vt.ASS_EXC_IMP)
            {
                var kind = uiCfgToIoCfg(m_activeList.data.attStyle, false);
                var arrFld = getArrIoFld(kind);
                var assparam = "main.ashx?_method=ASS_ImpFromExcel"
                            + "&IoCfgType=" + kind;
                           // + "&IoCfgXml=" + escape(objListToXmlStr(arrFld));
                var vHtml = '<BR/><form target="ASSET_ExcelIframe"  method="post" action="' + assparam + '" enctype="multipart/form-data" id="ASSET_ExcelForm">'
                    + $XML(param,"name") + '文件：<input type="file" name="upfile" />'
                    + '<input type="hidden" name="IoCfgXml" value="' + escape(objListToXmlStr(arrFld)) + '"/>'
                    + '</form>'
                    + '<iframe id="ASSET_ExcelIframe" name="ASSET_ExcelIframe" style="display:none" '
                    + ' onload="top.m_doc.getWndElem(' + m_idx + ').impFinish(this);"></iframe>';
               top.CtnDlg.show(vHtml, {title:$XML(param,"name"), width:300, height:100, arrbtn:[{text:"确定",onClick:impExcel},{text:"取消",onClick:function(dlg){dlg.close();}}]});
            }
            else if (id == m_vt.ASS_IF_EXP)
            {
                var kind = uiCfgToIoCfg(m_activeList.data.attStyle, true);
                var assparam = "CheckedAll=" + m_activeList.bchecked
                            + "&Ids=" + arId.join(",")
                            + "&Sql=" + encodeURIComponent(m_activeList.data.strSql)
                            + "&IoCfgType=" + kind;
                CtnTrans.Ajax("/main.ashx?_method=ASS_ExpToJk", "post", assparam,
                function(res)
                {
                    if(!CtnError.validate(res.responseText))
                    {
                        CtnError.show();
                        return;
                    }
                    CtnDlg.msg($XML(param,"name") + "成功！");
                },
                null,
                {loading:true,loadingText:"正在导出到接口..."});
            }
            else if (id == m_vt.ASS_IF_IMP)
            {
                var kind = uiCfgToIoCfg(m_activeList.data.attStyle, true);
                var assparam = "CheckedAll=" + m_activeList.bchecked
                            + "&Ids=" + arId.join(",")
                            + "&Sql=" + encodeURIComponent(m_activeList.data.strSql)
                            + "&IoCfgType=" + kind;
                CtnTrans.Ajax("/main.ashx?_method=ASS_ImpFromJk", "post", assparam,
                function(res, list)
                {
                    if(!CtnError.validate(res.responseText))
                    {
                        CtnError.show();
                        return;
                    }
                    CtnDlg.msg($XML(param,"name") + "成功！");
                    list.refresh();
                },
                m_activeList,
                {loading:true,loadingText:"正在从接口导入..."});
            }
        }
        break;
        case m_vt.ASS_PRJ_CF:
        {
            var prjId = getPrjId();
            if (!prjId)
            {
                return;
            }
            DpAction(AssCmd.CFBYPRJ, prjId);
            window.ASS_XDHD = function()
            {
                if(chgPrjStatus(prjId, $XML(param, "num1")))
                {
                    //refreshPrj();
                    top.CtnDlg.msg($XML(param, "name") + "成功!");
                }
            }
        }break;
        case m_vt.ASS_PRJINF_ZCMX:
        case m_vt.ASS_PRJINF_STQR:
        {
            var prjId = getPrjId();
            if (!prjId)
            {
                return;
            }
            if(chgPrjStatus(prjId, $XML(param, "num1")))
            {
                //refreshPrj();
                top.CtnDlg.msg($XML(param, "name") + "成功!");
            }
        }break;
        case m_vt.ASS_PRJ_JZFT2:
        case m_vt.ASS_PRJ_JZFT:
        {
            var prjId = getPrjId();
            if (!prjId)
            {
                return;
            }
            top.CtnDlg.form({sbfy:{text:"设备费用:",value:"0.00"},azfy:{text:"安装费用:",value:"0.00"},qtfy:{text:"其他费用:",value:"0.00"},zfy:{text:"总费用:",value:"0.00"}}
            ,{title:"价值均摊", width:200, height:180}
            ,function (inputs)
            {
                var strCmd = prjId;
                for (var k in inputs)
                {
                   strCmd += AssCmd.SEPARATOR + inputs[k].value;
                }
                DpAction(AssCmd.HBBYPRJ, strCmd);
                window.ASS_XDHD = function()
                {
                    if(chgPrjStatus(prjId, $XML(param, "num1")))
                    {
                        //refreshPrj();
                        top.CtnDlg.msg($XML(param, "name") + "成功!");
                    }
                }
            });
        }break;
        case m_vt.ASS_PRJ_GCZG:
        {
            var prjId = getPrjId();
            if (!prjId)
            {
                return;
            }
            DpAction(AssCmd.HBBYPRJ, prjId);
            window.ASS_XDHD = function()
            {
                if(chgPrjStatus(prjId, $XML(param, "num1")))
                {
                    //refreshPrj();
                    top.CtnDlg.msg($XML(param, "name") + "成功!");
                }
            }
        }break;
        case m_vt.ASS_ADD_ORGANIZE:
        {
            top.CtnDlg.open("Asset/AssDwGsZzGl.aspx",{title:"添加组织机构", width:400,height:350,arrbtn:[{text:"确定",onClick:AddOrganize},{text:"取消",onClick:function(dlg){dlg.close();}}]});
        }break;
        case m_vt.ASS_QUERY_ZDY:
        {
            var ent = getEntByStyle(parseInt($XML(m_activeList.data.xmlNode, "style")));
            top.QuerySql(ent[0],ent[1],
            function (sql){
                m_activeList.data.strSql = sql;
                m_activeList.refresh(1);
            });
        }break;
        default:
        {
            CtnDlg.msg("该功能未实现！");
        }
        break;
    }
}
function getPrjId()
{
    var prjId = 0;
    //在列表中拆分
    if (window.m_activeList)
    {
        var arr = m_activeList.getCheckedArr();
        if (arr.length != 1)
        {
            top.CtnDlg.msg("有且只能选择一个工程！");
            return 0;
        }
        prjId = m_activeList.getRow(arr[0]).getCell("ID0").text;
    }
    //在树中拆分
    else if (window.m_tree)
    {
        prjId = m_tree.getSelectedItem().data.ID0;
    }
    return prjId;
}
//弹出导入报表对话框
function impExcel(dlg)
{
    top.$("ASSET_ExcelForm").dlg = dlg;
    top.$("ASSET_ExcelForm").submit();
    dlg.element.style.display = "none";
    top.CtnDlg.loading("正在导入...");
    
}
//导入报表
function impFinish(elem)
{
    if (elem.ASSFLAG)//第一次是假的
    {
        top.CtnDlg.unloading();
        var text = elem.contentWindow.document.documentElement.innerText;
        if (text != "")
        {
            top.CtnDlg.error(text);
        }
        else
        {
            top.CtnDlg.msg("导入成功！");
            m_activeList.refresh();
        }
        top.$("ASSET_ExcelForm").dlg.close();
    }
    else
    {
        elem.ASSFLAG = true;
    }
}
//改变工程状态
function chgPrjStatus(prjId, status)
{
    //为0说明不改变状态
    if (status != 0)
    {
        var text = CtnTrans.getText("/main.ashx?_method=ASS_ChgPrjStatus", "post", "PrjId=" + prjId + "&Status=" + status);
        if (!CtnError.validate(text))
        {
            CtnError.show();
            return false;
        }
        refreshPrj();
    }
    return true;
}

function qrmsg(msg)
{
    top.CtnDlg.show(msg, {title:"提示", width:200, height:70,arrbtn:[{text:"确定"
    ,onClick:
    function(dlg)
    {
        dlg.close();
        window.ASS_XDHD();
        window.ASS_XDHD = null;
        var win = top.m_doc.getWndElem(m_vt.SYS_MAIN);
        if (win)
        {
            win.location.href = win.location.href.split("#")[0].split("?")[0] + "?" + new Date().getTime();
        }
    }}]});
}
function refreshPrj()
{
    //更新工程列表
    if (window.m_arrList)
    {
        for (var i = 0; i < m_arrList.length; i++)
        {
            if (m_arrList[i].data.attStyle == AssUiAttStyle.Prj)
            {
                m_arrList[i].refresh();
            }
        }
    }
    //更新向导页
    var win = top.m_doc.getWndElem(m_vt.SYS_MAIN);
    if (win)
    {
        win.location.href = win.location.href.split("#")[0].split("?")[0] + "?" + new Date().getTime();
    }
}
//添加组织机构
function AddOrganize(dlg)
{
    var win = dlg.wnd;
    var arr = m_activeList.getCheckedArr();
    var arrId = [];
    for (var i = 0; i < arr.length; i++)
    {
        arrId.push(m_activeList.getRow(arr[i]).getCell("ID0").text);
    }
    var params = "FkpIds=" + arrId.join(",");
    var ComPrvince = win.$("Select_ComPrvince");
    var Fcomp = win.$("Fcomp");
    var ManageDept = win.$("ManageDept");
    var FiliManage = win.$("FiliManage");
    var UseDept = win.$("UseDept");
    var filiUse = win.$("FiliManage");
    if (Fcomp.options.length < 1)
    {
        top.CtnDlg.msg("请选择市公司!");
        Fcomp.focus();
        return;
    }
    else
    {
        var str = Fcomp.options[Fcomp.selectedIndex].text;
        if (ComPrvince.options.length > 0)
        {
            str = ComPrvince.options[ComPrvince.selectedIndex].text + str;
        }
        params += "&Fcomp=" + escape(str);
    }
    if (ManageDept.options.length < 1)
    {
        top.CtnDlg.msg("请选择管理部门!");
        ManageDept.focus();
        return;
    }
    else
    {
        var str = ManageDept.options[ManageDept.selectedIndex].text;
        if (FiliManage.options.length > 0)
        {
            str += FiliManage.options[FiliManage.selectedIndex].text;
        }
        params += "&ManageDept=" + escape(str);
    }
    if (UseDept.options.length < 1)
    {
        top.CtnDlg.msg("请选择实物部门!");
        UseDept.focus();
        return;
    }
    else
    {
        var str = UseDept.options[UseDept.selectedIndex].text;
        if (filiUse.options.length > 0)
        {
            str += filiUse.options[filiUse.selectedIndex].text;
        }
        params += "&UseDept=" + escape(str);
    }
    if (win.$V("ManageMan") == "")
    {
        top.CtnDlg.msg("部门管理员不能为空!");
        win.$("ManageMan").focus();
        return;
    }
    else
    {
        params += "&ManageMan=" + escape(win.$V("ManageMan"));
    }
    if (win.$V("Select_SaveMan") == "")
    {
        top.CtnDlg.msg("实物管理员不能为空!");
        win.$("Select_SaveMan").focus();
        return;
    }
    else
    {
        params += "&SaveMan=" + escape(win.$V("Select_SaveMan"));
    }
    var text = CtnTrans.getText("/main.ashx?_method=ASS_AddOrganise", "post", params);
    if (!CtnError.validate(text))
    {
        CtnError.show();
        return false;
    }
    dlg.close();
    m_activeList.refresh();
}